﻿using System;
using System.Collections.Generic;
using System.IO;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Configuration.Enums;
using PSE.Deployment.SetupBase.Parameter;
using PSE.Deployment.Engine.CustomExceptions;
using PSE.Deployment.Engine.Interfaces;

namespace PSE.Deployment.SetupBase.Steps
{
    public class CreateShortcut : SetupStep<Configuration.Actions.CreateShortcut>
    {
        public CreateShortcut(IStepGroup parent, Group group, Configuration.Actions.CreateShortcut action, Parameter.Enums.SetupMode setupMode)
            : base(parent, group, action, setupMode)
        {
            this.ResetProgress(Action.Shortcuts.Count);
        }

        public override void Execute()
        {
            this.ResetProgress(Action.Shortcuts.Count);

            if (this.SetupMode == Parameter.Enums.SetupMode.Install)
            {
                CreateShortcuts();
            }
            else if (this.SetupMode == Parameter.Enums.SetupMode.Uninstall)
            {
                UninstallArtefacts();
            }
        }

        public override void Undo()
        {
            if (this.SetupMode == Parameter.Enums.SetupMode.Install)
            {
                UninstallArtefacts();
            }
        }

        protected void CreateShortcuts()
        {
            foreach (Shortcut shortcut in Action.Shortcuts)
            {
                if (this.IsStopped())
                    return;

                string iconLocation = string.Empty;
                string targetPath = SetupMainFactory.GetInstance().Parameter.InstallationPath + shortcut.Executable.Destination + shortcut.Executable.FileName;

                if (shortcut.Icon != null)
                {
                    iconLocation = SetupMainFactory.GetInstance().Parameter.InstallationPath + shortcut.Icon.Destination + shortcut.Icon.FileName;
                }

                foreach (SpecialFolder specialFolder in shortcut.SpecialFolders)
                {
                    string shortcutLink = string.Empty;

                    if (specialFolder == SpecialFolder.Desktop)
                        shortcutLink = PSE.Deployment.Util.Windows.GetFolderPath(PSE.Deployment.Util.Windows.SpecialFolder.Desktop);
                    else if (specialFolder == SpecialFolder.StartMenu)
                        shortcutLink = Path.Combine(PSE.Deployment.Util.Windows.GetFolderPath(PSE.Deployment.Util.Windows.SpecialFolder.StartMenu), shortcut.ShotcutFile.Destination);

                    shortcutLink = Path.Combine(shortcutLink, shortcut.ShotcutFile.FileName);

                    this.IncrementProgress(shortcutLink);

                    try
                    {
                        if (!Directory.Exists(Path.GetDirectoryName(shortcutLink)))
                            Directory.CreateDirectory(Path.GetDirectoryName(shortcutLink));

                        PSE.Deployment.Util.Windows.CreateShortcut(shortcutLink, iconLocation, targetPath, shortcut.Executable.Arguments);

                        this.AddUsedBy(shortcutLink);
                    }
                    catch
                    {
                        this.DecrementProgress(shortcutLink);
                        throw;
                    }
                }
            }
        }

        protected override void UninstallArtefact(string artefactName)
        {
            if (File.Exists(artefactName))
                File.Delete(artefactName);

            if (Directory.Exists(Path.GetDirectoryName(artefactName)))
                PSE.Deployment.Util.File.DeleteEmptyDirectories(SetupMainFactory.GetInstance().Parameter.InstallationPath, Path.GetDirectoryName(artefactName));
        }
    }
}
